package main

import (
	"log"
	"os"
	"somedata/core"
	"somedata/handler"
	"somedata/store"
	"strconv"
)

func main() {

	// 1 配置文件加载
	configFile := os.Getenv("CONFIG_FILE")
	if configFile == "" {
		configFile = "config.toml"
	}

	var debug bool
	debugEnv := os.Getenv("DEBUG")
	if debugEnv == "" {
		debug = true
	} else {
		debug, _ = strconv.ParseBool(os.Getenv("DEBUG"))
	}

	appConfig, err := core.LoadConfig(configFile)
	if err != nil {
		log.Fatal(err)
	}
	appConfig.Path = configFile
	if debug {
		_ = core.SaveConfig(appConfig)
	}

	// 2 创建服务对象
	appServer := core.NewServer(appConfig)

	// 插件初始化
	gormConfig := store.NewGormConfig()
	db, err := store.NewPostgres(gormConfig, appConfig)
	client, _ := store.NewRedisClient(appConfig)

	// 初始化服务
	appServer.Init(debug, client, db)

	// 路由注册
	locationHandler := handler.NewUserHandler(appServer, db)
	locationGroup := appServer.Engine.Group("/api/location/")
	locationGroup.GET("list", locationHandler.List)

	// 3 服务运行
	err = appServer.Run(db)
	if err != nil {
		log.Fatal(err)
	}

}
